<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.customcodingrule_delete"
          @click="handleDelete"
          >删除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from '@/api/system/customcodingrule'
import { mapGetters } from 'vuex'
import func from '@/util/func'

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      beginNumMax: null,
      beginNumMin: null,
      endNumMax: null,
      endNumMin: null,
      stepMax: null,
      stepMin: 1,
      serialNum: '',
      option: {
        labelWidth: 100,
        height: 'auto',
        align: 'center',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        dialogType: 'drawer',
        dialogWidth: '70%',
        selection: true,
        dialogClickModal: false,
        column: [
          {
            label: '编码名称',
            prop: 'codeName',
            overHidden: true,
            type: 'select',
            width: 180,
            dicUrl: '/api/blade-system/dict/dictionary?code=custom_coding_rule',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            rules: [
              {
                required: true,
                message: '请输入编码名称',
                trigger: 'blur'
              }
            ],
            editDisabled: true,
            span: 24
          },
          {
            label: '编码前缀',
            prop: 'prefixCode',
            rules: [
              {
                required: true,
                message: '请输入前缀',
                trigger: 'blur'
              }
            ],
            span: 24
          },
          {
            label: '中间类型 ',
            prop: 'midCode',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=code_mid_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            rules: [
              {
                required: true,
                message: '请输入中间类型',
                trigger: 'blur'
              }
            ],
            span: 24
          },
          {
            label: '流水号长度',
            prop: 'serialNumLen',
            type: 'number',
            minRows: 1,
            maxRows: 6,
            rules: [
              {
                required: true,
                message: '请输入流水号长度',
                trigger: 'blur'
              }
            ],
            span: 24,
            blur: ({ value }) => {
              if (func.isUndefined(value)) {
                value = 0
              } else {
                this.form.beginNum = null
                this.form.endNum = null
                this.form.step = null
              }
              if (value === 1) {
                this.beginNumMax = 8
                this.endNumMax = 9
              } else if (value === 2) {
                this.beginNumMax = 98
                this.endNumMax = 99
              } else if (value === 3) {
                this.beginNumMax = 998
                this.endNumMax = 999
              } else if (value === 4) {
                this.beginNumMax = 9998
                this.endNumMax = 9999
              } else if (value === 5) {
                this.beginNumMax = 99998
                this.endNumMax = 99999
              } else if (value === 6) {
                this.beginNumMax = 999998
                this.endNumMax = 999999
              }
            }
          },
          {
            label: '流水号起',
            prop: 'beginNum',
            controls: false,
            type: 'number',
            minRows: 1,
            rules: [
              {
                required: true,
                message: '请输入流水号起',
                trigger: 'blur'
              }
            ],
            hide: true,
            span: 8,
            blur: ({ value }) => {
              if (func.isUndefined(this.form.endNum)) {
                if (value > this.beginNumMax) {
                  this.form.beginNum = this.beginNumMax
                } else if (value < this.beginNumMin) {
                  this.form.beginNum = this.beginNumMin
                }
                this.form.endNum = Number(this.form.beginNum) + 1
                this.form.step = 1
              } else {
                if (value >= this.form.endNum) {
                  if (value > this.beginNumMax) {
                    this.form.beginNum = this.beginNumMax
                    this.form.endNum = this.endNumMax
                    this.form.step = 1
                  } else {
                    this.form.endNum = Number(this.form.beginNum) + 1
                  }
                } else if (value < this.beginNumMin) {
                  this.form.beginNum = this.beginNumMin
                }
              }
              this.stepMax = Number(this.form.endNum) - Number(this.form.beginNum)
            }
          },
          {
            label: '流水号止',
            prop: 'endNum',
            minRows: 2,
            controls: false,
            type: 'number',
            rules: [
              {
                required: true,
                message: '请输入流水号止',
                trigger: 'blur'
              }
            ],
            hide: true,
            span: 8,
            blur: ({ value }) => {
              if (func.isUndefined(this.form.beginNum)) {
                if (value > this.endNumMax) {
                  this.form.endNum = this.endNumMax
                } else if (value < this.endNumMin) {
                  this.form.endNum = this.endNumMin
                }
                this.form.beginNum = Number(this.form.endNum) - 1
                this.form.step = 1
              } else {
                if (value <= this.form.beginNum) {
                  if (value < this.endNumMin) {
                    this.form.endNum = this.endNumMin
                    this.form.beginNum = this.beginNumMin
                    this.form.step = 1
                  } else {
                    this.form.beginNum = Number(this.form.endNum) - 1
                  }
                } else if (value > this.endNumMax) {
                  this.form.endNum = this.endNumMax
                }
              }
              this.stepMax = Number(this.form.endNum) - Number(this.form.beginNum)
            }
          },
          {
            label: '流水号步长',
            prop: 'step',
            minRows: 1,
            controls: false,
            type: 'number',
            rules: [
              {
                required: true,
                message: '流水号步长',
                trigger: 'blur'
              }
            ],
            span: 8,
            blur: ({ value }) => {
              this.stepMax = Number(this.form.endNum) - Number(this.form.beginNum)
              if (value > this.stepMax) {
                this.form.step = this.stepMax
              } else if (value < this.stepMin) {
                this.form.step = 1
              }
            }
          },
          {
            label: '编码示例',
            prop: 'example',
            width: 150,
            display: false,
            overHidden: true
          },
          {
            label: '编码说明',
            prop: 'codeDesc',
            width: 180,
            rules: [
              {
                max: 500,
                message: '请输入编码说明',
                trigger: 'blur'
              }
            ],
            span: 24
          },
          {
            label: '是否启用',
            prop: 'status',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=yes_no',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            rules: [
              {
                required: true,
                message: '请输入状态',
                trigger: 'blur'
              }
            ]
          }
        ]
      },
      data: []
    }
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.customcodingrule_add, false),
        viewBtn: this.vaildData(this.permission.customcodingrule_view, false),
        delBtn: this.vaildData(this.permission.customcodingrule_delete, false),
        editBtn: this.vaildData(this.permission.customcodingrule_edit, false)
      }
    },
    ids() {
      let ids = []
      this.selectionList.forEach(ele => {
        ids.push(ele.id)
      })
      return ids.join(',')
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          loading()
          window.console.log(error)
        }
      )
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          loading()
          console.log(error)
        }
      )
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.$refs.crud.toggleSelection()
        })
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data
        })
      }
      done()
    },
    beforeClose(done) {
      this.beginNumMax = null
      this.beginNumMin = null
      this.endNumMax = null
      this.endNumMin = null
      this.stepMax = null
      this.stepMin = 1
      this.serialNum = ''
      done()
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.page.currentPage = 1
      this.onLoad(this.page, params)
      done()
    },
    selectionChange(list) {
      this.selectionList = list
    },
    selectionClear() {
      this.selectionList = []
      this.$refs.crud.toggleSelection()
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    refreshChange() {
      this.onLoad(this.page, this.query)
    },
    onLoad(page, params = {}) {
      this.loading = true
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data
        this.page.total = data.total
        this.data = data.records
        this.loading = false
        this.selectionClear()
      })
    }
  }
}
</script>

<style></style>
